package com.eureka.auth;

import com.alibaba.fastjson.JSONObject;
import com.eureka.auth.service.AuthService;
import com.eureka.baseconfig.annotation.Auth;
import com.eureka.baseconfig.annotation.AuthInfo;
import com.eureka.bclass.base.BaseLoginUserInfo;
import com.eureka.bclass.base.BaseResult;
import com.eureka.bclass.constant.SysKeyConstant;
import com.eureka.bclass.enums.SysKeyEnum;
import com.eureka.bclass.util.LoggerUtil;
import com.eureka.bclass.util.StrUtil;
import com.eureka.bclass.util.VerifyCodeUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.MessageFormat;

/**
 * <p>
 * 登录认证相关
 * </p>
 *
 * @author lixing_java@163.com
 * @since 2021-06-06
 */
@RestController
@RequestMapping("auth")
public class AuthController {
    @Resource
    private AuthService authService;

    @Value("${custom.domain}")
    private String customDomain;

    /**
     * @Description 图片验证码
     * @Author 李星
     * @Date 2019/3/10 15:38
     * @Param [request, response]
     */
    @GetMapping("verifyCode")
    public void verifyCode(HttpServletRequest request, HttpServletResponse response) {
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setContentType("image/jpeg");

        // 生成随机验证码
        String verifyCode = StrUtil.genRandomStr("mix",4).toLowerCase();
        // 存入session对象
        request.getSession().setAttribute(MessageFormat.format(SysKeyEnum.LOGIN_USER_GRAPHICVERIFICATIONCODE.getKey(), SysKeyConstant.PROJECTNAME, null), verifyCode.toLowerCase());

        // 生成验证码图片并响应到页面
        ServletOutputStream servletOutputStream = null;
        try {
            servletOutputStream = response.getOutputStream();
            VerifyCodeUtil.outputImage_2(verifyCode, 110, 38, servletOutputStream);
        } catch (IOException e) {
            LoggerUtil.error(MessageFormat.format("输出验证码出错：{0}", e.getMessage()));
        } finally {
            if (servletOutputStream != null) {
                try {
                    servletOutputStream.close();
                } catch (IOException e) {
                    LoggerUtil.info("获取验证码模块-关闭ServletOutputStream对象出错：" + e);
                }
            }
        }
    }

    /**
     * @Description 登录
     * @Author lixing
     * @Date 2021/6/17 9:11
     * @Param [baseLoginUserInfo]
     * @return com.eureka.bclass.base.BaseResult<java.lang.String>
     */
    @PostMapping("login")
    public BaseResult<JSONObject> login(@RequestBody BaseLoginUserInfo baseLoginUserInfo, HttpServletRequest request) {
        return authService.login(baseLoginUserInfo, request);
    }

    /**
     * @Description 获取用户密码
     * @Author lixing
     * @Date 2021/6/19 22:11
     * @Param [baseLoginUserInfo]
     * @return com.eureka.bclass.base.BaseResult<java.lang.String>
     */
    @GetMapping("getUserPwd")
    @Auth
    public synchronized BaseResult<BaseLoginUserInfo> getUserPwd(@AuthInfo BaseLoginUserInfo baseLoginUserInfo) {
        return authService.getUserPwd(baseLoginUserInfo);
    }
}
